package com.leantech.android.browser.views

import android.content.Context
import android.util.AttributeSet
import com.qmuiteam.qmui.widget.QMUIRadiusImageView

class QMUIAspectRatioRadiusImageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : QMUIRadiusImageView(context, attrs, defStyleAttr) {

    private var ratio = 0f // width/height

    fun setRadio(width: Int, height: Int) {
        this.ratio = width.toFloat() / height.toFloat()
        requestLayout()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        if (ratio > 0) {
            val width = MeasureSpec.getSize(widthMeasureSpec)
            val height = (width / ratio).toInt()
            super.onMeasure(
                MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
            )
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        }
    }
}